package com.qixin.modules.ea.controller;

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.*;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.qixin.modules.ea.entity.*;
import com.qixin.modules.ea.mapper.EaEntertainMapper;
import com.qixin.modules.ea.service.*;
import com.qixin.modules.ea.vo.EaTravelPage;
import com.qixin.modules.ea.vo.ListPageVo;
import com.qixin.modules.eaApplication.entity.EaEntertainApply;
import com.qixin.modules.eaApplication.service.IEaEntertainApplyService;
import com.qixin.modules.eaBasic.entity.EaTask;
import com.qixin.modules.eaBasic.mapper.EaTaskMapper;
import com.qixin.modules.eaBasic.vo.EaTaskVo;
import com.qixin.modules.eaBasic.vo.IdVo;
import com.qixin.modules.rzInvoice.entity.EaBill;
import com.qixin.modules.rzInvoice.service.IEaBillService;
import com.qixin.modules.rzInvoice.service.impl.EaBillServiceImpl;
import com.qixin.utils.PreciseCompute;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.EmptyUtil;
import org.jeecg.common.util.FillRuleUtil;
import org.jeecg.modules.activiti.entity.ActZTask;
import org.jeecg.modules.activiti.mapper.ActZTaskMapper;
import org.jeecg.modules.activity.service.ActivityCommonService;
import org.jeecg.modules.nc.entity.NcPersonnel;
import org.jeecg.modules.nc.service.INcPersonnelService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import com.qixin.modules.ea.vo.EaEntertainPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
* @Description: 招待费报销单
* @Author: qixin-boot
* @Date:   2021-07-06
* @Version: V1.0
*/
@Api(tags="app招待费报销单")
@RestController
@RequestMapping("/app/api/eaEntertain")
@Slf4j
public class ApiEaEntertainController {
    @Autowired
    private IEaEntertainService eaEntertainService;
    @Autowired
    private IEaBookDetailService eaBookDetailService;
    @Autowired
    private IEaApplyDetailService eaApplyDetailService;
    @Autowired
    private IEaEntertainReceptionActivityService eaEntertainReceptionActivityService;
    @Autowired
    private INcPersonnelService ncPersonnelService;
    // 工作流公共执行类
    @Autowired
    private ActivityCommonService activityCommonService;
    @Autowired
    private IEaEntertainApplyService eaEntertainApplyService;
    @Autowired
    private EaBillServiceImpl eaBillService;
    @Autowired
    private ActZTaskMapper actZTaskMapper;
    @Autowired
    private EaEntertainMapper eaEntertainMapper;

//    /**
//     * 查询招待费报销单信息列表
//     *
//     * @param listPageVo
//     * @return Result
//     */
//    @ApiOperation(value="招待费报销单信息列表-查询", notes="招待费报销单信息列表-查询")
//    @PostMapping(value = "/list")
//    public Result<?> list(@RequestBody ListPageVo listPageVo) {
//        Map<String,Object> resutlList = eaEntertainService.list(listPageVo);
//        return Result.OK(resutlList);
//    }

    /**
     * 分页列表查询
     *
     * @param eaEntertain
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "招待费报销单-分页列表查询", notes = "招待费报销单-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(EaEntertain eaEntertain,
                                   @RequestParam(name = "billDateBegin", required = false) String billDateBegin,
                                   @RequestParam(name = "billDateEnd", required = false) String billDateEnd,
                                   @RequestParam(name="billCode", required = false) String billCode,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<EaEntertain> queryWrapper = QueryGenerator.initQueryWrapper(eaEntertain, req.getParameterMap());
        //add-begin-by wujinbiao for 添加查询条件 列表排序，创建人
        if (org.apache.commons.lang3.StringUtils.isNotBlank(billDateBegin)) {
            queryWrapper.ge("bill_date", billDateBegin);
        }
        if (org.apache.commons.lang3.StringUtils.isNotBlank(billDateEnd)) {
            queryWrapper.le("bill_date", billDateEnd);
        }
        if(org.apache.commons.lang3.StringUtils.isNotBlank(billCode)){
            // 单据编号模糊查询
            queryWrapper.like("bill_no",billCode);
        }
        queryWrapper.orderByDesc("create_time");
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        queryWrapper.eq("create_by", loginUser.getId());
        //add-end-by wujinbiao for 添加查询条件
        queryWrapper.orderByDesc("create_time");
        Page<EaEntertain> page = new Page<EaEntertain>(pageNo, pageSize);
        IPage<EaEntertain> pageList = eaEntertainService.page(page, queryWrapper);
        List<EaEntertain> records = pageList.getRecords();
        for (EaEntertain eaEntertain1 : records) {
            NcPersonnel ncPersonnel = ncPersonnelService.queryById(eaEntertain1.getPersonId());
            if (ncPersonnel != null) {
                eaEntertain1.setPersonIdName(ncPersonnel.getName());
            }
        }
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param eaEntertainPage
     * @return
     */
    @AutoLog(value = "招待费报销单-添加")
    @ApiOperation(value = "招待费报销单-添加", notes = "招待费报销单-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody EaEntertainPage eaEntertainPage) {
        EaEntertain eaEntertain = new EaEntertain();
        BeanUtils.copyProperties(eaEntertainPage, eaEntertain);

        // update-begin-Autor: wjb date: 2022-02-09 10:47 for: 生成单据编号
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("prefix","ZDBX"); // 编码前缀
        Object result = FillRuleUtil.executeRule("common_code", jsonObj);
        eaEntertain.setBillNo(result.toString());
        // update-end-Autor: wjb date: 2022-02-09 10:47 for: 生成单据编号
        eaEntertain.setState(0);
        eaEntertainService.saveMain(eaEntertain, eaEntertainPage.getEaBookDetailList(), eaEntertainPage.getEaApplyDetailList(), eaEntertainPage.getEaEntertainReceptionActivities());
        // update-begin-author wjb 2022-02-16 15:50 for返回收款人中文
        if (StringUtils.isNotBlank(eaEntertain.getRecPersonId())){
            NcPersonnel ncPersonnel = ncPersonnelService.queryById(eaEntertain.getRecPersonId());
            if(ncPersonnel != null){
                eaEntertain.setRecPersonIdName(ncPersonnel.getName());
            }
        }
        // update-end-author wjb 2022-02-16 15:50 for返回收款人中文
        //add-begin-by 2022-02-07 wjb for 返回主表、子表数据给前端
        EaEntertainPage resultPage = new EaEntertainPage();
        BeanUtils.copyProperties(eaEntertain, resultPage);
        resultPage.setEaEntertainReceptionActivities(eaEntertainPage.getEaEntertainReceptionActivities());
        resultPage.setEaBookDetailList(eaEntertainPage.getEaBookDetailList());
//      resultPage.setEaApplyDetailList(eaEntertainPage.getEaApplyDetailList());
        //add-end-by 2022-02-07 wjb for 返回主表、子表数据给前端

        //update-begin-author wjb 2022-02-10 15:49 返回关联的申请单实体集合
        QueryWrapper<EaEntertainApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ea_entertain_id",eaEntertain.getId());
        EaEntertainApply eaEntertainApply = eaEntertainApplyService.getOne(queryWrapper);
        ArrayList<EaEntertainApply> eaEntertainApplyList = new ArrayList<>();
        eaEntertainApplyList.add(eaEntertainApply);
        resultPage.setEaApplyDetailList(eaEntertainApplyList);
        //update-end-author wjb 2022-02-10 15:49 返回关联的申请单实体集合

        return Result.OK(resultPage);
    }

//    /**
//     *  编辑
//     *
//     * @param eaEntertainPage
//     * @return
//     */
//    @AutoLog(value = "招待费报销单-编辑")
//    @ApiOperation(value="招待费报销单-编辑", notes="招待费报销单-编辑")
//    @PostMapping(value = "/edit")
//    public Result<?> edit(@RequestBody EaEntertainPage eaEntertainPage) {
//        EaEntertain eaEntertain = new EaEntertain();
//        BeanUtils.copyProperties(eaEntertainPage, eaEntertain);
//        EaEntertain eaTravelEntity = eaEntertainService.getById(eaEntertain.getId());
//        if(eaTravelEntity==null) {
//            return Result.error("未找到对应数据");
//        }
//
//        //判断单据是否已经被提交
//        EaEntertain eaEntertain1 = eaEntertainService.getById(eaEntertainPage.getId());
//        String status = EmptyUtil.checkNull(eaEntertain1.getState());   //当状态为1审批中,2已审批时,提示单据状态已变更,请刷新
//        if(status.equals("1") || status.equals("2")){
//            return Result.error("单据状态已变更,请刷新");
//        }
//
//        eaEntertain.setSaveType("add");    //保存
//        Map<String,Object> returnMap = eaEntertainService.updateMain(eaEntertain, eaEntertainPage.getEaBookDetailList(),eaEntertainPage.getEaApplyDetailList(),eaEntertainPage.getEaBorrowSubtractList(),eaEntertainPage.getEaEntertainReceptionActivities());
//
//        if(returnMap.get("code").equals("-1")){
//            return Result.error(EmptyUtil.checkNull(returnMap.get("message")));
//        }else{
//            returnMap.remove("code");
//            returnMap.remove("message");
//            return Result.OK(returnMap);
//        }
//    }

    /**
     * 编辑
     *
     * @param eaEntertainPage
     * @return
     */
    @AutoLog(value = "招待费报销单-编辑")
    @ApiOperation(value = "招待费报销单-编辑", notes = "招待费报销单-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody EaEntertainPage eaEntertainPage) {
        EaEntertain eaEntertain = new EaEntertain();
        BeanUtils.copyProperties(eaEntertainPage, eaEntertain);
        EaEntertain eaEntertainEntity = eaEntertainService.getById(eaEntertain.getId());
        if (eaEntertainEntity == null) {
            return Result.error("未找到对应数据");
        }
        eaEntertain.setSaveType("add");
        eaEntertainService.updateMain(eaEntertain, eaEntertainPage.getEaBookDetailList(), eaEntertainPage.getEaApplyDetailList(), eaEntertainPage.getEaEntertainReceptionActivities());
        // update-begin-author wjb 2022-02-16 15:50 for返回收款人中文
        if (StringUtils.isNotBlank(eaEntertain.getRecPersonId())){
            NcPersonnel ncPersonnel = ncPersonnelService.queryById(eaEntertain.getRecPersonId());
            if(ncPersonnel != null){
                eaEntertain.setRecPersonIdName(ncPersonnel.getName());
            }
        }
        // update-end-author wjb 2022-02-16 15:50 for返回收款人中文

        //add-begin-by 2022-02-07 wjb for 返回主表、子表数据给前端
        EaEntertainPage resultPage = new EaEntertainPage();
        BeanUtils.copyProperties(eaEntertain, resultPage);
        resultPage.setEaEntertainReceptionActivities(eaEntertainPage.getEaEntertainReceptionActivities());
//        resultPage.setEaApplyDetailList(eaEntertainPage.getEaApplyDetailList());
        resultPage.setEaBookDetailList(eaEntertainPage.getEaBookDetailList());
        //add-end-by 2022-02-07 wjb for 返回主表、子表数据给前端

        //update-begin-author wjb 2022-02-10 15:49 返回关联的申请单实体集合
        QueryWrapper<EaEntertainApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ea_entertain_id",eaEntertain.getId());
        EaEntertainApply eaEntertainApply = eaEntertainApplyService.getOne(queryWrapper);
        ArrayList<EaEntertainApply> eaEntertainApplyList = new ArrayList<>();
        eaEntertainApplyList.add(eaEntertainApply);
        resultPage.setEaApplyDetailList(eaEntertainApplyList);
        //update-end-author wjb 2022-02-10 15:49 返回关联的申请单实体集合

        return Result.OK(resultPage);
    }

//    /**
//     *   通过id删除
//     *
//     * @param idVo
//     * @return
//     */
//    @AutoLog(value = "招待费报销单-通过id删除(未提交状态可以删除)")
//    @ApiOperation(value="招待费报销单-通过id删除", notes="招待费报销单-通过id删除")
//    @PostMapping(value = "/delete")
//    public Result<?> delete(@RequestBody IdVo idVo) {
//        String id = EmptyUtil.checkNull(idVo.getId());
//        EaEntertain eaTravelEntity = eaEntertainService.getById(id);
//        if(eaTravelEntity==null) {
//            return Result.error("未找到对应数据");
//        }
//        Map<String,Object> returnMap = eaEntertainService.delMain(id);
//        if(returnMap.get("code").equals("-1")){
//            String message = EmptyUtil.checkNull(returnMap.get("message"));
//            return Result.error(message);
//        }else{
//            return Result.OK("删除成功!");
//        }
//    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "招待费报销单-通过id删除")
    @ApiOperation(value = "招待费报销单-通过id删除", notes = "招待费报销单-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        QueryWrapper<EaEntertain> params = new QueryWrapper<>();
        params.eq("id",id);
        EaEntertain entertain = eaEntertainService.getOne(params);
        if(entertain != null && entertain.getState() > 0){
            return Result.error("该单据已发起流程,不可删除!");
        }
        eaEntertainService.delMain(id);
        return Result.OK("删除成功!");
    }


//    /**
//     * 通过id查询
//     *
//     * @param idVo
//     * @return
//     */
//    @ApiOperation(value="招待费报销单-通过id查询", notes="招待费报销单-通过id查询")
//    @PostMapping(value = "/queryById")
//    public Result<?> queryById(@RequestBody IdVo idVo) {
//        String id = EmptyUtil.checkNull(idVo.getId());
//        EaEntertain eaEntertain = eaEntertainService.getById(id);
//        if(eaEntertain==null) {
//            return Result.error("未找到对应数据");
//        }
//        Map<String, Object> resutlList = eaEntertainService.selectById(id);
//        return Result.OK(resutlList);
//
//    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "招待费报销单-通过id查询", notes = "招待费报销单-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        EaEntertainPage resultPage = new EaEntertainPage();
        EaEntertain eaEntertain = eaEntertainService.getById(id);
        if (eaEntertain == null) {
            return Result.error("未找到对应数据");
        }

        String personIdName = "";
        //add-begin-by wjb for 报销人中文返回 子表数据
        //查询报销人中文
        if (org.apache.commons.lang3.StringUtils.isNotBlank(eaEntertain.getPersonId())) {
            NcPersonnel ncPersonnel = ncPersonnelService.queryById(eaEntertain.getPersonId());
            if (ncPersonnel != null) {
                personIdName = ncPersonnel.getName();
                eaEntertain.setPersonIdName(personIdName);
            }
        }
        // update-begin-author wjb 2022-02-16 15:50 for 返回收款人中文
        if (StringUtils.isNotBlank(eaEntertain.getRecPersonId())){
            NcPersonnel ncPersonnel = ncPersonnelService.queryById(eaEntertain.getRecPersonId());
            if(ncPersonnel != null){
                eaEntertain.setRecPersonIdName(ncPersonnel.getName());
            }
        }
        // update-end-author wjb 2022-02-16 15:50 for 返回收款人中文
        BeanUtils.copyProperties(eaEntertain, resultPage);
        List<EaBookDetail> eaBookDetails = eaBookDetailService.selectByMainId(eaEntertain.getId());
        List<EaApplyDetail> eaApplyDetails = eaApplyDetailService.selectByMainId(eaEntertain.getId());
        List<EaEntertainReceptionActivity> eaEntertainReceptionActivities = eaEntertainReceptionActivityService.selectByMainId(eaEntertain.getId());
        resultPage.setEaBookDetailList(eaBookDetails);
//        eaEntertainPage.setEaApplyDetailList(eaApplyDetails);
        resultPage.setEaEntertainReceptionActivities(eaEntertainReceptionActivities);
        //add-end-by wjb for 报销人中文返回 子表数据
        //update-begin-author wjb 2022-02-10 15:49 返回关联的申请单实体集合
        QueryWrapper<EaEntertainApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ea_entertain_id",eaEntertain.getId());
        EaEntertainApply eaEntertainApply = eaEntertainApplyService.getOne(queryWrapper);
        ArrayList<EaEntertainApply> eaEntertainApplyList = new ArrayList<>();
        eaEntertainApplyList.add(eaEntertainApply);
        resultPage.setEaApplyDetailList(eaEntertainApplyList);
        //update-end-author wjb 2022-02-10 15:49 返回关联的申请单实体集合

        return Result.OK(resultPage);
    }

//    /**
//     * 招待费报销单审批/退回
//     *
//     * @param eaTaskVo
//     * @return
//     */
//    @AutoLog(value = "招待费报销单审批/退回")
//    @ApiOperation(value="招待费报销单审批/退回", notes="招待费报销单审批/退回")
//    @PostMapping(value = "/audit")
//    public Result<?> edit(@RequestBody EaTaskVo eaTaskVo) {
//        Map<String,Object> returnMap = eaEntertainService.audit(eaTaskVo);
//        return Result.OK(returnMap);
//    }


//    /**
//     * 发票更新
//     *
//     * @param billIds
//     * @return
//     */
//    @AutoLog(value = "发票更新")
//    @ApiOperation(value = "发票更新", notes = "发票更新")
//    @PutMapping(value = "/updeteFpState")
//    @Transactional
//    public Result<?> updeteFpState(@RequestParam(name = "billIds", required = true) String billIds) {
//        if (!StringUtils.isEmpty(billIds)) {
//            String[] ids = billIds.split(",");
//            // 发票金额
//            double amount = 0;
//            // 实际付款金额
//            double actualEaAmt = 0;
//            // 业务表id
//            String billId = "";
//            List<EaBookDetail> eaBookDetails = new ArrayList<>();
//            List<EaBill> eaBills = new ArrayList<>();
//            for (String id : ids) {
//                // 需要更新的发票明细行数据
//                EaBookDetail eaBookDetail = eaBookDetailService.selectById(id);
//                if(eaBookDetail == null || "".equals(eaBookDetail)){
//                    return Result.error("找不到对应数据");
//                }
//                // 需要更新的发票数据
//                EaBill eaBill = eaBillService.getById(eaBookDetail.getFpid());
//                billId = eaBookDetail.getBillId();
//                // 发票被引用
//                if ("1".equals(eaBill.getIsused()) && eaBookDetail.getBillId() != null) {
//                    amount = PreciseCompute.add(amount,eaBookDetail.getAmount().doubleValue());
//                    actualEaAmt = PreciseCompute.add(actualEaAmt,eaBookDetail.getActualEaAmt().doubleValue());
//                    // 关联的表单id置为空
//                    eaBookDetail.setBillId("");
//                    // 发票状态更新
//                    eaBill.setIsused("0");
//
//                    eaBookDetails.add(eaBookDetail);
//                    eaBills.add(eaBill);
//                }
//            }
//            // 取消费用明细行和业务表单的关系
//            eaBookDetailService.updateBatchById(eaBookDetails);
//            // 修改发票已报销状态
//            eaBillService.updateBatchById(eaBills);
//
//            // 报销单减去实际付款金额、报销金额
//            if (billId != ""){
//                EaEntertain eaEntertain =  eaEntertainService.queryById(billId);
//
//                // 修改待办列表的还款金额
//                Map<String, Object> params = new HashMap<>();
//                params.put("form_id",billId);
//                List<EaTask> eaTasks = eaTaskMapper.selectByMap(params);
//                if(eaTasks.size() > 0){
//                    // 修改任务列表 对应的报销金额
//                    EaTask eaTask = eaTasks.get(0);
//                    eaTask.setTotalFee(new BigDecimal(PreciseCompute.sub(eaEntertain.getEaAmt().doubleValue(),amount)));
//                    eaTaskMapper.updateById(eaTask);
//                }
//                // 报销金额 修改
//                eaEntertain.setEaAmt(new BigDecimal(PreciseCompute.sub(eaEntertain.getEaAmt().doubleValue(),amount)));
//                // 实际付款金额 修改
//                eaEntertain.setActualPayAmt(new BigDecimal(PreciseCompute.sub(eaEntertain.getActualPayAmt().doubleValue(),actualEaAmt)));
//                eaEntertainService.updateById(eaEntertain);
//            }
//        }
//
//        return Result.OK();
//    }

    /**
     * 发票更新
     *
     * @param billIds
     * @return
     */
    @AutoLog(value = "发票更新")
    @ApiOperation(value = "发票更新", notes = "发票更新")
    @PutMapping(value = "/updeteFpState")
    @Transactional
    public Result<?> updeteFpState(@RequestParam(name = "billIds", required = true) String billIds) {
        if (!org.apache.commons.lang.StringUtils.isEmpty(billIds)) {
            String[] ids = billIds.split(",");
            // 发票金额
            double amount = 0;
            // 实际付款金额
            double actualEaAmt = 0;
            // 业务表id
            String billId = "";
            List<EaBookDetail> eaBookDetails = new ArrayList<>();
            List<EaBill> eaBills = new ArrayList<>();
            for (String id : ids) {
                // 需要更新的发票明细行数据
                EaBookDetail eaBookDetail = eaBookDetailService.selectById(id);
                if(eaBookDetail == null || "".equals(eaBookDetail)){
                    return Result.error("找不到对应数据");
                }
                // 需要更新的发票数据
                EaBill eaBill = eaBillService.getById(eaBookDetail.getFpid());
                billId = eaBookDetail.getBillId();
                // 发票被引用
                if ("1".equals(eaBill.getIsused()) && eaBookDetail.getBillId() != null) {
                    amount = PreciseCompute.add(amount,eaBookDetail.getAmount().doubleValue());
                    actualEaAmt = PreciseCompute.add(actualEaAmt,eaBookDetail.getActualEaAmt().doubleValue());
                    // 关联的表单id置为空
                    eaBookDetail.setBillId("");
                    // 发票状态更新
                    eaBill.setIsused("0");

                    eaBookDetails.add(eaBookDetail);
                    eaBills.add(eaBill);
                }
            }
            // 取消费用明细行和业务表单的关系
            eaBookDetailService.updateBatchById(eaBookDetails);
            // 修改发票已报销状态
            eaBillService.updateBatchById(eaBills);

            // 报销单减去实际付款金额、报销金额
            if (billId != ""){
                EaEntertain eaEntertain =  eaEntertainMapper.selectById(billId);

                // 修改待办列表的还款金额
                Map<String, Object> params = new HashMap<>();
                params.put("table_id",billId);
                List<ActZTask> actZTasks = actZTaskMapper.selectByMap(params);
                if(actZTasks.size() > 0){
                    // 修改任务列表 对应的报销金额
                    ActZTask actZTask = actZTasks.get(0);
                    actZTask.setTotalFee(new BigDecimal(PreciseCompute.sub(eaEntertain.getEaAmt().doubleValue(),amount)));
                    actZTaskMapper.updateById(actZTask);
                }
                // 报销金额 修改
                eaEntertain.setEaAmt(new BigDecimal(PreciseCompute.sub(eaEntertain.getEaAmt().doubleValue(),amount)));
                // 实际付款金额 修改
                eaEntertain.setActualPayAmt(new BigDecimal(PreciseCompute.sub(eaEntertain.getActualPayAmt().doubleValue(),actualEaAmt)));
                eaEntertainService.updateById(eaEntertain);
            }
        }

        return Result.OK();
    }

}
